/*点击棋盘按钮触发函数*/
function boardBtnClick(self) {
	var idStr = self.id;
	var boardName = idStr.substring(0,5);
	if (boardName=='myBtn'){
		//如果是添加飞机
		if (!del){
			var index = idStr.substr(5,idStr.length);
			var x = index%10;
			var y = parseInt(index/10);
			if (myFlightCount>=3){
				mui.toast('飞机数量已达到3架\n不能再放置飞机');
				return;
			}
			if (!isLegal(x,y,direction,myBoard)){
				mui.toast('飞机位置不合法');
				return;
			}
			myFlight[myFlightCount][0] = x;
			myFlight[myFlightCount][1] = y;
			myFlight[myFlightCount][2] = direction;
			for (var i = 0; i < 10; i++) {
				var xi = x + dirInc[direction][i][0];
				var yi = y + dirInc[direction][i][1];
				var indexi = yi*10 + xi;
				var tmpBtn = mui('#myBtn'+indexi)[0];
				tmpBtn.style.backgroundColor = 'white';
				tmpBtn.style.borderStyle = flightStyle[direction][i];
				myBoard[xi][yi] = true;
			}
			myFlightCount++;
		}
		else{
			var index = idStr.substr(5,idStr.length);
			var x = index%10;
			var y = parseInt(index/10);
			for (var i = 0; i < myFlightCount; i++){
				if (myFlight[i][0]==x&&myFlight[i][1]==y){
					for (var j = 0; j < 10; j++) {
						var xi = x + dirInc[myFlight[i][2]][j][0];
						var yi = y + dirInc[myFlight[i][2]][j][1];
						var indexi = yi*10 + xi;
						var tmpBtn = mui('#myBtn'+indexi)[0];
						tmpBtn.style.backgroundColor = boardBackgroundColor;
						tmpBtn.style.borderStyle = 'solid solid solid solid';
						myBoard[xi][yi] = false;
					}
					myFlightCount--;
					for (var j = i; j < myFlightCount; j++){
						myFlight[j] = myFlight[j+1].concat();
					}
					return;
				}
			}
		}
	}
	else {
		if (!fighting) return;
		var index = aim[1]*10+aim[0];
		var lastBtn = mui('#enemyBtn'+index)[0];
		if ((!shootMap[aim[0]][aim[1]])&&(!markMap[aim[0]][aim[1]])){
			lastBtn.style.backgroundColor = boardBackgroundColor;
		}
		index = self.id.substr(8, self.id.length);
		aim[0] = index%10;
		aim[1] = parseInt(index/10);
		if (shootMap[aim[0]][aim[1]]) return;
		self.style.backgroundColor = 'green';
	}
}

/*点击功能按钮触发函数*/
function funBtnClick(self) {
	self.disabled = true;
	if (self.id=='upBtn') {
		direction = 0;
		del = false;
		mui('#leftBtn')[0].disabled = false;
		mui('#rightBtn')[0].disabled = false;
		mui('#downBtn')[0].disabled = false;
		mui('#delBtn')[0].disabled = false;
		return;
	}
	else if (self.id=='leftBtn') {
		direction = 1;
		del = false;
		mui('#upBtn')[0].disabled = false;
		mui('#rightBtn')[0].disabled = false;
		mui('#downBtn')[0].disabled = false;
		mui('#delBtn')[0].disabled = false;
		return;
	}
	else if (self.id=='rightBtn') {
		direction = 2;
		del = false;
		mui('#upBtn')[0].disabled = false;
		mui('#leftBtn')[0].disabled = false;
		mui('#downBtn')[0].disabled = false;
		mui('#delBtn')[0].disabled = false;
		return;
	}
	else if (self.id=='downBtn') {
		direction = 3;
		del = false;
		mui('#upBtn')[0].disabled = false;
		mui('#leftBtn')[0].disabled = false;
		mui('#rightBtn')[0].disabled = false;
		mui('#delBtn')[0].disabled = false;
		return;
	}
	else if (self.id=='delBtn') {
		del = true;
		mui('#upBtn')[0].disabled = false;
		mui('#leftBtn')[0].disabled = false;
		mui('#rightBtn')[0].disabled = false;
		mui('#downBtn')[0].disabled = false;
		return;
	}
}

/*点击准备按钮触发函数*/
function readyBtnClick(self) {
	if (myFlightCount<3){
		mui.toast('飞机放置未满3架\n不能开始');
		return;
	}
	mui('#upBtn')[0].disabled = 'disabled';
	mui('#leftBtn')[0].disabled = 'disabled';
	mui('#rightBtn')[0].disabled = 'disabled';
	mui('#downBtn')[0].disabled = 'disabled';
	mui('#delBtn')[0].disabled = 'disabled';
	mui('#readyBtn')[0].disabled = 'disabled';
	for (var i = 0; i < 100; i++) {
		mui('#myBtn'+i)[0].disabled = 'disabled';
	}
	//AI初始化
	ai.init();
	//AI布置飞机
	ai.creatFlights();
	//开始战斗
	fighting = true;
	mui.toast('英雄，开始战斗！请你先发射炮弹！');
	return;
}

/*点击射击按钮触发函数*/
function shootBtnClick(self) {
	if (!fighting) return;
	if (shootMap[aim[0]][aim[1]]){
		mui.toast('已经轰炸过该坐标');
		return;
	}
	var index = aim[1]*10+aim[0];
	var enemyBtn = mui('#enemyBtn'+index)[0];
	enemyBtn.style.backgroundColor = 'blue';
	if (enemyBoard[aim[0]][aim[1]]){
		enemyBtn.style.backgroundColor = 'yellow';
		for (var i = 0; i < enemyFlightCount; i++){
			if (enemyFlight[i][0]==aim[0]&&enemyFlight[i][1]==aim[1]){
				enemyBtn.style.backgroundColor = 'red';
				enemyDestroy++;
				if (enemyDestroy==3){
					enemyBoardUncover();
					setTimeout("mui.alert('恭喜你赢得了比赛！(第'+fightRound+'回合)', 'Flight Fight')", 1000);
					return;
				}
				break;
			}
		}
	}
	shootMap[aim[0]][aim[1]] = true;
	aim = [0,0];
	//AI轰炸
	var aiAim = ai.aiShoot();
	console.log('ai shoot> x:'+aiAim[0]+' y:'+aiAim[1]);
	//判断是否炸到
	var index = aiAim[1]*10+aiAim[0];
	var myBtn = mui('#myBtn'+index)[0];
	//如果炸到，进一步判断是机身还是机头
	if (myBoard[aiAim[0]][aiAim[1]]){
		//先假定是机身
		var hitHead = false;
		//遍历3个飞机头
		for (var i = 0; i < myFlightCount; i++){
			//如果轰炸到飞机头，则置标志位为true
			if (myFlight[i][0]==aiAim[0]&&myFlight[i][1]==aiAim[1]){
				hitHead = true;
				break;
			}
		}
		//如果打到头
		if (hitHead){
			myBtn.style.backgroundColor = 'red';
			flightLost++;
			//判断是否输掉
			if (flightLost==3){
				enemyBoardUncover();
				setTimeout("mui.alert('很遗憾,你输掉了比赛...(第'+fightRound+'回合)', 'Flight Fight')", 1000);
			}
			//AI接收轰炸结果
			ai.rcvResult(aiAim[0], aiAim[1], 3);
		}
		//
		else{
			myBtn.style.backgroundColor = 'yellow';
			//AI接收轰炸结果
			ai.rcvResult(aiAim[0], aiAim[1], 2);
		}
	}
	else{
		myBtn.style.backgroundColor = 'blue';
		//AI接收轰炸结果
		ai.rcvResult(aiAim[0], aiAim[1], 1);
	}
	fightRound++;
}

/*点击标记按钮触发函数*/
function markBtnClick(self){
	if (!fighting) return;
	if (shootMap[aim[0]][aim[1]]){
		mui.toast('已经轰炸过该坐标');
		return;
	}
	var index = aim[1]*10+aim[0];
	var enemyBtn = mui('#enemyBtn'+index)[0];
	if (markMap[aim[0]][aim[1]]){
		enemyBtn.style.backgroundColor = boardBackgroundColor;
		markMap[aim[0]][aim[1]] = false;
	}
	else{
		enemyBtn.style.backgroundColor = 'white';
		markMap[aim[0]][aim[1]] = true;
	}
	return;
}

/*点击重玩按钮触发函数*/
function replayBtnClick(self){
	boardInit();
	/*游戏控制全局变量*/
	myFlightCount = 0; //已布置飞机数量
	enemyFlightCount = 0;
	direction = 0; //飞机方向
	del = false; //删除还是添加
	fighting = false; //是否开始战斗
	aim = [0,0]; //瞄准
	enemyDestroy = 0; //摧毁敌军数量
	flightLost = 0; //失去飞机数量
	fightRound = 0; //回合
	count = 0;
	
	/*定义己方飞机和敌方飞机*/
	myFlight = Array(3);
	for (var i = 0; i < 3; i++) {
		myFlight[i] = Array(3);
	}
	enemyFlight = Array(3);
	for (var i = 0; i < 3; i++) {
		enemyFlight[i] = Array(3);
	}
	//定义己方棋盘和敌方棋盘
	for (var i = 0; i < 10; i++) {
		for (var j = 0; j < 10; j++) {
			myBoard[i][j] = false;
		}
	}
	for (var i = 0; i < 10; i++) {
		for (var j = 0; j < 10; j++) {
			enemyBoard[i][j] = false;
		}
	}
	
	for (var i = 0; i < 10; i++) {
		for (var j = 0; j < 10; j++) {
			shootMap[i][j] = false;//瞄准地图
			markMap[i][j] = false;//标记地图
		}
	}
	mui.alert('英雄，开始新的一轮战斗！请你布置自己的3架飞机！', 'Flight Fight');
}

/*点击帮助按钮触发函数*/
function helpBtnClick(self){
	mui.openWindow('help.html');
}
